<?php

include ("configs/config.php");



if ( $_SESSION['LOGIN_NAME'] <> "admin" ) 
	{
	header("Location: login-admin.php");
	exit();
	}




$tpl = new XTemplate("./template/tpl_admin.html");
$tpl->parse("main.body.menu");

$query=("select * from groups;");
$result=mysql_query($query);

$error=0;

if ($_GET["clicked"]==1)
{
	$name=$_GET["name"];
	$id=$_GET["id_group"];
		
	if ($name=="")
	{
		$error++;
		$msg_error.="Please enter a name for this category</br>";
	}
	else
	{	
		$query='update groups set name="'.$name.'" where id='.$id.';';
	
		$result=mysql_query($query);
		header("Location: edit-group.php");
		exit();
		}
}	

if ($_GET['delete']<>0)
{
	$id=$_GET['delete'];
	$query=("delete  from groups where id=$id");
	$result=mysql_query($query);
	
	if (mysql_affected_rows()<1)
		{
			$error++;
			$msg_error.="There was a problem with the database! Please come back in 10 minutes";
		}	
	else
	{
		header("Location: edit-group.php");
		exit();
	}
}


if ($_GET['add_clicked'])
{
	if ($_GET['name']<>"")
		{
			$name=addslashes($_GET['name']);
			$query=('insert into groups (name) values("'.$name.'");');
			mysql_query($query);
			if (mysql_affected_rows()<1)
			{
					$error++;
					$msg_error="There was a problem with the database! Please come back in 10 minutes";
				
			}
			else
			{
				header("Location: edit-group.php");
				exit();
			}
		}
		else
		{
			$error++;
			$msg_error.="-please enter a name for this group</br>";
		}
}



$query=("select * from groups order by name ;");
$result=mysql_query($query);

if (mysql_num_rows($result)<1)
{
	$msg_error="There was a problem with the database! Please come back in 10 minutes";
	$error++;
}	
else	
{

	while ($group=mysql_fetch_array($result))
	{
		$tpl->assign("id",$group['id']);
		$tpl->assign("group",$group['name']);
		$tpl->assign("msg_error",$msg_error);
		$tpl->parse("main.body.edit_group.row");
		
	};

}


$tpl->assign("error",$msg_error);
$tpl->parse("main.body.edit_group.error");
$tpl->parse("main.body.edit_group");
$tpl->parse("main.body");

$tpl->parse("main");
$tpl->out("main");
?>